//
//  DrawView.m
//  day6作业 画板
//
//  Created by tyw on 15/8/5.
//  Copyright (c) 2015年 tyw. All rights reserved.
//

#import "DrawView.h"

@implementation DrawView {
    NSMutableArray* _lineArray;     //存储线条，里面的item也是数组，就是点集合
    NSMutableArray* _pointArray;    //存储点，是点的集合，用完后存入_lineArray
    
    NSMutableArray* _colorArray;    //存储之前线条的颜色
    NSMutableArray* _widthArray;    //存储之前线条的宽度
}

- (instancetype)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        self.userInteractionEnabled = YES;
        self.backgroundColor = [UIColor whiteColor];
        _lineArray = [[NSMutableArray alloc] init];
        _pointArray = [[NSMutableArray alloc] init];
        _colorArray = [[NSMutableArray alloc] init];
        _widthArray = [[NSMutableArray alloc] init];
        _color = [UIColor blackColor];
        _lineWidth = 1;
    }
    
    return self;
}
//撤销，将各数组最后一个元素删除即可，然后重绘画板
- (void)undo {
    [_lineArray removeLastObject];
    [_pointArray removeLastObject];
    [_colorArray removeLastObject];
    [_widthArray removeLastObject];
    
    [self setNeedsDisplay];
}
//清屏，将各个数组清空然后重绘画板即可
- (void)clear {
    [_lineArray removeAllObjects];
    [_pointArray removeAllObjects];
    [_colorArray removeAllObjects];
    [_widthArray removeAllObjects];
    
    [self setNeedsDisplay];
}
//当触摸开始，记录下第一个起始点startPoint并将它加入到点数组，作为第一个元素
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    UITouch* touch = [touches anyObject];
    CGPoint startPoint = [touch locationInView:self];
    NSString* startPointStr = NSStringFromCGPoint(startPoint);
    [_pointArray addObject:startPointStr];
}
//当触摸点移动时，实时记录触摸点的位置，将它们存入点数组
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
    UITouch* touch = [touches anyObject];
    
    CGPoint point = [touch locationInView:self];
    NSString* pointStr = NSStringFromCGPoint(point);
    
    [_pointArray addObject:pointStr];
    
    //实时调用display方法，刷新画板
    [self setNeedsDisplay];
}
//手指抬起，触摸结束。拷贝点数组，新建一个数组，加入到线数组。并将点数组清空，以承载下一次画的点集合
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
    //这里一定要这么写，相当于复制了一个数组，不能直接等于，否则两数组地址一样，就是同一个
    NSArray* array = [NSArray arrayWithArray:_pointArray];
    [_lineArray addObject:array];
    [_pointArray removeAllObjects];
    
    //每画完一条线，就把当前线的颜色和线宽这一数据记录下来存入数组
    UIColor* color = _color;
    [_colorArray addObject:color];
    NSInteger width = _lineWidth;
    [_widthArray addObject:[NSString stringWithFormat:@"%li",width]];
}

- (void)drawRect:(CGRect)rect {
    //获得当前上下文
    CGContextRef context = UIGraphicsGetCurrentContext();
    
    //如果线条数组有元素，说明前面已经有画好的线条了。在开始画新的线条前，要把之前的线条先画好，显示在屏幕上的效果就是前面的线条还在，已经在画新的线条了
    if (_lineArray.count > 0) {
        //写一个循环，将前面的线一条一条先绘制出来
        for (int i = 0; i < _lineArray.count; i++) {
            NSArray* pointArray = _lineArray[i];
            if (pointArray.count > 0) { //如果点数组有数据，说明有线，才绘制
                [self drawLine:context withPointArray:pointArray withColor:_colorArray[i] andLineWidth:[_widthArray[i] integerValue]];
            }
        }
    }
    
    //当点集合中有元素时，说明已经开始画新的线条了
    if (_pointArray.count > 0) {
        [self drawLine:context withPointArray:_pointArray withColor:_color andLineWidth:_lineWidth];
    }
}

- (void)drawLine:(CGContextRef)context withPointArray:(NSArray*)pointArray withColor:(UIColor*)color andLineWidth:(NSInteger)lineWidth {
    CGContextBeginPath(context);        //开始一条路径
    CGContextSetLineJoin(context, kCGLineJoinRound);    //线条拐角样式，圆润
    CGContextSetLineCap(context, kCGLineCapRound);      //线条两头样式，圆润
    
    CGPoint startPoint = CGPointFromString(pointArray[0]);
    CGContextMoveToPoint(context, startPoint.x, startPoint.y);  //设置起始点
    
    //添加绘制点
    for (int i = 1; i < pointArray.count; i++) {
        CGPoint point = CGPointFromString(pointArray[i]);
        CGContextAddLineToPoint(context, point.x, point.y);
    }
    
    
    [color set];                               //线条颜色,为什么setFill就不行了？
    CGContextSetLineWidth(context, lineWidth);     //宽度
    
//    CGContextDrawPath(context, kCGPathFillStroke);        //万恶啊，这句留着就会有雨刷效果
    
    CGContextStrokePath(context);
}


@end
